package com.aspirecn.core.commons;

import java.net.MalformedURLException;
import org.apache.commons.validator.routines.UrlValidator;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;

public class LinkUtils {
	
	private String url;
	
	public LinkUtils(String url) {
		this.url = url;
	}

	/**
	 * 从URL或者URL的一部分中获取参数值
	 * @param urlorquary
	 * @param name
	 * @return
	 */
	public static String getParameterValue( String urlorquary, String name )
	{
		if( StringUtils.containsIgnoreCase(urlorquary, name+"=") )
		{
			//System.out.println( urlorquary );
			String str = StringUtils.replace(urlorquary, name+"=", StringUtils.lowerCase(name)+"=");
			//System.out.println( name+"=" );
			//System.out.println( StringUtils.lowerCase(name)+"=" );
			String retStr = StringUtils.substringBetween(str, StringUtils.lowerCase(name)+"=", "&");
			
			if( StringUtils.isEmpty(retStr) )
			{
				retStr = StringUtils.substringAfter(str, StringUtils.lowerCase(name)+"=");
			}
			
			return retStr;			
		}
		
		return null;
	}
	
	
	
	public static String removeParameter( String urlorquary, String name )
	{
		if( StringUtils.contains(urlorquary, name + "=") )
		{
			String value = StringUtils.substringBetween(urlorquary, name + "=", "&");
			if( StringUtils.isEmpty(value) )
			{
				value = StringUtils.substringAfter(urlorquary, name + "=");
			}
			
			String retStr = urlorquary;
			retStr = StringUtils.remove(retStr, name + "=" + value + "&");
			retStr = StringUtils.remove(retStr, "&" + name + "=" + value);
			retStr = StringUtils.remove(retStr, "?" + name + "=" + value);
			
			return retStr;
			
		}
		
		return urlorquary;
	}
	
	public LinkUtils removeParameter(String name) {
		url = removeParameter(url, name);
		return this;
	}
	
	
	
	public static String addParameter( String urlorquary, String name, String value )
	{
		// #top 类似这样的链接，不添加参数
		if (StringUtils.contains(urlorquary, "#")
				&& (!UrlValidator.getInstance().isValid(urlorquary))) {
			return urlorquary;
		}
		
		String retStr = urlorquary;
		if( StringUtils.contains(retStr, "?" + name + "=") || StringUtils.contains(retStr, "&" + name + "=") )
		{
			retStr = removeParameter(retStr, name);
		}
		
		if( StringUtils.contains(retStr, "?") )
		{
			retStr = retStr + "&" + name + "=" + value;
		}
		else
		{
			retStr = retStr + "?" + name + "=" + value;
		}
		
		return retStr;
	}
	
	public LinkUtils addParameter(String name, String value ) {
		url = addParameter(url, name, value);
		return this;
	}
	
	/**
	 * 获取当前访问url
	 * @param request
	 * @return
	 */
	public static String getCorrentURL( HttpServletRequest request )
	{
		String url = request.getRequestURI().toString();
		String queryString = request.getQueryString();
		
		String retUrl = "";
		if( StringUtils.isBlank(queryString) )
		{
			retUrl = url;
		}
		else
		{
			retUrl = url + "?" + queryString;
		}
		
		return retUrl;
	}
	
	
	public String getUrl() {
		return url;
	}

	public static void main( String[] args ) throws MalformedURLException
	{
		String url = "#fffff";
		
//		URL urlObj = new URL(url);
//		
//		System.out.println( urlObj.getHost() );
//		
//		System.out.println( urlObj.getPath() );
		
		String urlt = new LinkUtils(url)
					.addParameter("a", "a")
					.addParameter("b", "b")
					.removeParameter("p")
					.getUrl();
		System.out.println(urlt);
	}
}
